import asyncio
import json
import time

from curl_cffi import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs
import hashlib

with open('h5st514.js', 'r', encoding='utf-8') as f:
    js = f.read()

js_com = execjs.compile(js)


def sha_256(message):
    return hashlib.sha256(message.encode('utf-8')).hexdigest()


def convert_cookie_dict_to_string(cookie_dict):
    # Join each key-value pair with '=' and then join all pairs with '; '
    cookie_string = '; '.join([f"{key}={value}" for key, value in cookie_dict.items()])
    return cookie_string


url = "https://api.m.jd.com/"


def generate_h5st(params):
    # 解析原始 body
    body_str = params['body']
    body = json.loads(body_str) if isinstance(body_str, str) else body_str
    print("body:::",body)
    # 创建用于 SHA256 加密的 boddy
    boddy = {
        "keyword": body['keyword'],
        "wq": body['wq'],
        "stock": body['stock'],
        "pvid": body['pvid'],
        "isList": body['isList'],
        "page": body['page'],
        "s": body['s'],
        "click": body['click'],
        "log_id": body['log_id'],
        "show_items": body['show_items'],
    }

    # 创建用于生成 h5st 的 a 对象
    a = {
        "appid": params.get('appid', 'search-pc-java'),
        "body": sha_256(json.dumps(boddy, separators=(',', ':'))),
        "clientVersion": params.get('clientVersion', '1.0.0'),
        "client": params.get('client', 'pc'),
        "functionId": params.get('functionId', 'pc_search_s_new'),
        "t": int(time.time() * 1000),  # 使用当前时间戳
    }

    # 调用 JS 的 PSign.sign 方法生成 h5st
    h5st = js_com.call('getH5st', a)

    return h5st


params = {
    "appid": "search-pc-java",
    "functionId": "pc_search_s_new",
    "client": "pc",
    "clientVersion": "1.0.0",
    "t": str(int(time.time() * 1000)),
    "body": "{\"keyword\":\"美达信\",\"wq\":\"美达信\",\"stock\":\"1\",\"pvid\":\"28649d535351496ab98c3a47b45f3844\",\"isList\":0,\"page\":\"3\",\"s\":\"54\",\"click\":\"0\",\"log_id\":\"1751541397598.4591\",\"show_items\":\"\"}",
    "loginType": "3",
    "uuid": "143920055.1744782034530924475269.1744782034.1751443836.1751541353.51",
    "area": "19_1607_0_0",
    "h5st": "20250703191645191;i9gigpw3g22020h1;f06cc;tk03w9eb11bc618nfIvGCOCwagXPOt4KiLIOEucPoOFXZ9AL3m9spacmCTD1HVd7IYtPAZ6wObuKkAVzPU2REVRSzACa;6728587b33be25fa5454516cdd4d34f8;5.1;1751541404191;ri_uxFOm9Ooi8uLi8S4i3t3VFRYgFlsm0msSIlsmOGuj4qrm0mMTLhImOuMsCm8hMtbgNhog4a7i2aoi1ebW_u7hJZog4W7WNdoV3mrhNlsm0msSo94VMZ4RMusmk_Mm8mohIRrgMRri2KLV2m7W1e4i1iYi5ebi1eIiMJbh9qImOGLm7pIRAp4WMusmk_ciBuMgMebRMlsmOGujMSLj92ch4xZVCJIVPZrUMuMgMWHmOuMsCmsZ_6oYcZKaDVLaol7SMuMgM64TK1YW8lsmOGujMm7iAJ4ZMuMgMWoSMusmk_cPOuMs8uMgMqbi5lImOusmOGuj8qrm0msi9aHWMusmOuMsCObjOGLm8qbRMlsmOusmk_MmWNrg2lYSWpLialsm0mcT-dITNlHmOusmOGuj_uMgMObRMlsmOusmk_siOGLm3aHWMusmOuMsCS7iOGLm4aHWMusmOuMsCurm0mch5lImOusmOGuj_uMgMebRMlsmOusmk_sh7uMgMibRMlsmOusmk_Mm42ciAuLmOGLm9aHWMusmOuMsCurm0m8U3lsmOusmk_chOGLm79ImOusmOGuj_uMgM_ImOusmOGuj_uMgMe4RMusmOuMsztMgMeITJdnQJlsmOGujxtsmkm8WINbh2q4h7u7WNZ4hPdIUMuMgMmrSMusmOuMsztMgMunSMusmk_Mm6WrQOCrh42YUXt8g_2si9usZgt8S3xoVAJ4ZMuMgMqYR7lsmOG_Q;000d201b586eecf79b2d6500c9e2290c;tenjKJKT-JoRL1YRI9MT-J4S8ZIZ61YVF94WCeHTJJoTL9cQKxIWCeYU_tXW",
    "x-api-eid-token": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX2AABHKIAAAAACACGHDJDXP2NHYX"
}

h5st = generate_h5st(params)
print(h5st)
print(len(h5st))
params['h5st'] = h5st
headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "origin": "https://search.jd.com",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://search.jd.com/",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
    "x-referer-page": "https://search.jd.com/Search",
    "x-rp-client": "h5_1.0.0"
}
cookies = {
    "shshshfpa": "cba64f54-9bca-2a86-6d3b-5d0b2d2fda52-1744782036",
    "shshshfpx": "cba64f54-9bca-2a86-6d3b-5d0b2d2fda52-1744782036",
    "qrsc": "3",
    "pinId": "briEibRb-h1YgxkG81uaz7V9-x-f3wj7",
    "pin": "jd_7eac037a858fc",
    "unick": "jd_8js7q5tn2s7n2h",
    "_tp": "aO5XtogPiplRTZPyN6P5Vkd5JC%2FQJe7kEmh0g0oTaNI%3D",
    "_pst": "jd_7eac037a858fc",
    "__jdu": "1744782034530924475269",
    "pt_st": "1_Bn_8OJx9-DbefcPAdRwrE8ivlSHGM8s5HYCK940Ozx0ubwNc5sWh1vBavxlYNhOSkmRP39ujYL4Fp0-P1VRO0NiA6puM50SVh_gYGPFP-tTtNoFjBLWHOnVvkLZWyathzPNNkIia2hD0Hh_ryYrr_yoExG7S-IcKCJXyx6Y7HNQTkH_0_dfU-6pW8xw6WFdKTwqg",
    "__jdv": "76161171|direct|-|none|-|1751443835699",
    "areaId": "19",
    "ipLoc-djd": "19-1607-0-0",
    "PCSYCityID": "CN_440000_440300_0",
    "mail_times": "2%2C1",
    "jsavif": "1",
    "rkv": "1.0",
    "mba_muid": "1744782034530924475269",
    "x-rp-evtoken": "mGW9U4qbzsaBdCMe70m9pFIZOdeXQmpVZtDIlO62n7eiJVrz4WFiC4t_4zRLV7hK1g4HQeR-psES-p9fJ0lBLw%3D%3D",
    "mba_sid": "17515438353051097348138.2",
    "3AB9D23F7A4B3CSS": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX2ATBD6QAAAAADLV2NG3RFEMA4QX",
    "_gia_d": "1",
    "wlfstk_smdl": "mysoxpa6905ekazsu9qcqt8idgxcmzs8",
    "TrackID": "1EQGh009FEH_vpveAb3mtE1QbC-dszCI35OBWrg5PtINtQYJKnJWV0857GhWIAG4vJsmQ6YAhpyM7BrM-gK11AYo1aeSnBkZJ2OUdfxz_52TrBi1pPOgdv04EJArRbBtl",
    "thor": "2B3FAB022DDA7FFF772855629D82E8BA86B6B654FC776E1930AA7FEB1EB3100E5303995060FFE7385C165E798AA058BDAB769299D9BB5A894DC43907BB12E823BA2028B2910B4FDAC453BCA5F6631FA069BFF4BAD75D1EF335EC30651683D345DECC536A2FCA8380A56E9E4AFE7A809B3525D2327F20F24BAFCD705DCA9C0C09691C7352397C7F67C6AEE6F3C327DD70CF335FC247715A49D8409BDE652E9926",
    "flash": "3_vICYoprz7DLZ6bbZz3DG_CbO80GvFwpVRMrGd4dR6ckBXEZQkWVrILHl7TrXwwMXjMOvdPb0v7n93c801CD-6BRgUOYHZlk6epO14zSr7ApU2rD2TL-3tZeh-GJRCKjPTSHPc2Hn2Odwmz4WnNH2Na1jGJV1q0OFKZYjA84dAgsHXHy6_YzOUq**",
    "light_key": "AASBKE7rOxgWQziEhC_QY6ya3DVGcl5aLvGC8x57fQnvMJ7dVTENCaZwxaG9FX6lix2leQNo",
    "ceshi3.com": "000",
    "__jda": "143920055.1744782034530924475269.1744782034.1751541353.1751543833.52",
    "__jdb": "143920055.5.1744782034530924475269|52.1751543833",
    "__jdc": "143920055",
    "xapieid": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX2ATBD6QAAAAADLV2NG3RFEMA4QX",
    "shshshfpb": "BApXSlAou0_JAUOFEaY3EMt_yNHyW6WuIBgY5Jxl99xJ1MmR54oG2",
    "sdtoken": "AAbEsBpEIOVjqTAKCQtvQu17tr91EQOD5suIwJuW4fHcvjgqMYqrkIKYuE0wW_07Fpo-0GnwqdWcTVYsILHkCa78IrlYvdLOThMNdxdsjI4Ri1Gg7RmvHlC3YubDQ72z",
    "3AB9D23F7A4B3C9B": "GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBE"
}
response = requests.get(url, headers=headers, cookies=cookies, params=params)

print(response.text)
print(response)
